n = int(input())
dp = [0] * 10  # dp[i]: 以i结尾的最长子序列的长度
s = list(map(str, input().split()))
ans = 0

for i in range(n):
    head = int(s[i][0])
    tail = int(s[i][-1])
    dp[tail] = max(dp[tail], dp[head] + 1)
    ans = max(ans, dp[tail])

print(n - ans)